<?php
// 最终检查：确认菜单数据和用户权限
$host = '127.0.0.1';
$port = 3306;
$database = 'qiandao';
$username = 'root';
$password = 'root';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 最终诊断 ===\n\n";
    
    // 1. 检查所有一级菜单
    echo "1. 一级菜单列表:\n";
    $stmt = $pdo->query("SELECT id, title, node, status, sort FROM system_menu WHERE pid = 0 ORDER BY sort DESC, id ASC");
    $topMenus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($topMenus as $menu) {
        echo "  ID:{$menu['id']} {$menu['title']} (状态:{$menu['status']}, 排序:{$menu['sort']})\n";
    }
    
    // 2. 检查admin用户
    echo "\n2. Admin用户信息:\n";
    $stmt = $pdo->query("SELECT id, username, authorize, status FROM system_user WHERE username = 'admin'");
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
    echo "  ID:{$user['id']}, 用户名:{$user['username']}, 状态:{$user['status']}\n";
    echo "  授权:" . ($user['authorize'] ? $user['authorize'] : '(空)') . "\n";
    
    // 3. 检查配置
    echo "\n3. 系统配置:\n";
    $stmt = $pdo->query("SELECT name, value FROM system_config WHERE name IN ('super_user', 'app_map')");
    $configs = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($configs as $config) {
        echo "  {$config['name']}: {$config['value']}\n";
    }
    
    // 4. 建议
    echo "\n4. 诊断结果:\n";
    if ($topMenus[0]['title'] === '微信管理') {
        echo "  ✓ 微信管理菜单在第一位\n";
    }
    
    $hasSystemMenu = false;
    foreach ($topMenus as $menu) {
        if ($menu['title'] === '系统管理') {
            $hasSystemMenu = true;
            if ($menu['status'] == 1) {
                echo "  ✓ 系统管理菜单存在且已启用\n";
            } else {
                echo "  ✗ 系统管理菜单已禁用！\n";
            }
            break;
        }
    }
    
    if (!$hasSystemMenu) {
        echo "  ✗ 系统管理菜单不存在！\n";
    }
    
    if ($user['id'] == 10000) {
        echo "  ✓ Admin是超级管理员(ID:10000)\n";
    }
    
    echo "\n5. 解决方案:\n";
    echo "  请在浏览器中:\n";
    echo "  1. 完全退出登录\n";
    echo "  2. 清除浏览器缓存和Cookie\n";
    echo "  3. 关闭浏览器\n";
    echo "  4. 重新打开浏览器\n";
    echo "  5. 访问 http://127.0.0.1:8000\n";
    echo "  6. 使用 admin/admin 登录\n";
    echo "  7. 应该能看到'系统管理'菜单了\n";
    
} catch (PDOException $e) {
    echo "错误: " . $e->getMessage() . "\n";
}

